package com.example.cbl;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import androidx.appcompat.app.AppCompatActivity;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private ImageView ivBack, ivContacts, ivAdd;
    private EditText etSearch;
    private ListView lvMessages;
    private List<MessageItem> messageList;
    private MessageAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ivBack     = findViewById(R.id.iv_back_action);
        ivContacts = findViewById(R.id.iv_contacts_action);
        ivAdd      = findViewById(R.id.iv_add_action);
        etSearch   = findViewById(R.id.et_search_box);
        lvMessages = findViewById(R.id.lv_message_list);

        ivBack.setOnClickListener(v -> finish());
        ivContacts.setOnClickListener(v ->
                Toast.makeText(MainActivity.this, "点击了：联系人", Toast.LENGTH_SHORT).show()
        );
        ivAdd.setOnClickListener(v ->
                Toast.makeText(MainActivity.this, "点击了：添加", Toast.LENGTH_SHORT).show()
        );


        etSearch.setOnClickListener(v -> etSearch.setHint(""));

        initMessages();

        adapter = new MessageAdapter(this, messageList);
        lvMessages.setAdapter(adapter);

        lvMessages.setOnItemClickListener((parent, view, position, id) -> {
            MessageItem item = messageList.get(position);
            Toast.makeText(MainActivity.this,
                    "点击消息：" + item.getTitle(),
                    Toast.LENGTH_SHORT).show();

        });
    }
    private void initMessages() {
        messageList = new ArrayList<>();


        messageList.add(new MessageItem(
                R.drawable.cainiao,
                "消息盒子",
                "你的总资产收益已更新，点击查看详情",
                "10:11",
                true
        ));


        messageList.add(new MessageItem(
                R.drawable.qianbao,
                "关注动态",
                "用花呗买东西，退款的钱去哪？",
                "5月27日",
                true
        ));


        messageList.add(new MessageItem(
                R.drawable.robot,
                "2周前的消息",
                "以下是两周前收到的消息",
                "",
                false
        ));


        messageList.add(new MessageItem(
                R.drawable.robot,
                "朋友推荐",
                "朋友推荐的宝藏都在这里",
                "3月4日",
                false
        ));


        messageList.add(new MessageItem(
                R.drawable.tongzhi,
                "生日提醒",
                "亲爱的运郁，生日快乐 ~",
                "3月2日",
                false
        ));


        messageList.add(new MessageItem(
                R.drawable.xindongfang,
                "生活号",
                "花呗：毕业来花呗，领毕业额度和专属福利",
                "",
                false
        ));


        messageList.add(new MessageItem(
                R.drawable.xinxi,
                "新消息",
                "有新的通知，请查看",
                "2月20日",
                true
        ));


        messageList.add(new MessageItem(
                R.drawable.yangsheng,
                "养生中心",
                "健康资讯更新",
                "1月15日",
                false
        ));


        messageList.add(new MessageItem(
                R.drawable.youzan,
                "优惠赞赏",
                "您有一笔新的赞赏",
                "12月30日",
                true
        ));


        messageList.add(new MessageItem(
                R.drawable.youzann,
                "优惠赞赏",
                "感谢您的支持",
                "",
                false
        ));
    }


}
